// 爬取固定的数据
import { exit } from "process";
import color from "./color";
import htt from "./htt"
import sugar from "./sugar";

export default function(url:string,headers:any,web:any,all:any){
    web.regular.choose = sugar(web.regular.choose,"",all);
    web.regular.clear = sugar(web.regular.clear,"",all);

    return new Promise(resizeBy=>{
        let html:any ="";
        console.log("开始请求：",color("绿色",url))
        htt(url).get(url,{headers},(res:any)=>{
            res.on("data",(a:Buffer)=>html+=a)
            res.on("end",_=>{
                // 开始执行正则
                html = html.match(new RegExp(web.regular.choose,"g"))?.[web.subscript || 0].replace(new RegExp(web.regular.clear,"g"),"");
                // 再提取一下网址
                resizeBy(html.match(/^(((ht|f)tps?):\/\/)?[\w-]+(\.[\w-]+)+([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?$/)?.[0] || html)
                return true
            })
        })
    })
}